function checkPhone(phone) {
	var reg = /^1[34578]\d{9}$/;
	return reg.test(phone);
};

function checkEmail(email) {
	var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
	return reg.test(email);
};

function checkPassword(pwd) {
	//	var reg = /^1[34578]\d{9}$/;
	//	return reg.test(pwd);
	return true;
};

function login() {
	var phoneOrEmail = document.getElementsByName('UserNameLogin')[0].value;
	var pwd = document.getElementsByName('UserPasswordLogin')[0].value;
	if(checkPhone(phoneOrEmail)) {
		ajaxLoginByPhone(phoneOrEmail, pwd);
	} else if(checkEmail(phoneOrEmail)) {
		ajaxLoginByEmail(phoneOrEmail, pwd);
	} else {
		alert("请输入正确的手机号或邮箱！");
	}
}

function ajaxLoginByEmail(email, pwd) {
	var xmlhttp;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			//			alert(xmlhttp.responseText);
			if(xmlhttp.responseText == 100) {
//				window.open('index.php', '_self');
				location.reload();
			} else {
				alert("登录失败！错误信息：\n邮箱或密码不正确");
			}
		}
	}
	var json_request = '{ "UserEmail":"' + email + '" , "UserPassword":"' + pwd + '" }';
	xmlhttp.open("POST", "user.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send("Login=" + json_request);
}

function ajaxLoginByPhone(phone, pwd) {
	var xmlhttp;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			//			alert(xmlhttp.responseText);
			if(xmlhttp.responseText == 100) {
//				window.open('index.php', '_self');
				location.reload();
			} else {
				alert("登录失败！错误信息：\n手机号或密码不正确");
			}
		}
	}
	var json_request = '{ "UserPhone":"' + phone + '" , "UserPassword":"' + pwd + '" }';
	xmlhttp.open("POST", "user.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send("Login=" + json_request);
}

function ajaxRegister(userphone, userpwd, username, usergender, useremail) {
	var xmlhttp;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			//			alert(xmlhttp.responseText);
			if(xmlhttp.responseText == 100) {
				alert("注册成功！自动登录！");
//				window.open('index.php', '_self');
				location.reload();
			} else if(xmlhttp.responseText == 101) {
				alert("注册失败！错误信息：\n该手机号已被注册！");
			} else if(xmlhttp.responseText == 102) {
				alert("注册失败！错误信息：\n该邮箱已被注册！");
			} else {
				alert("注册失败！错误信息：\n" + xmlhttp.responseText);
			}
		}
	}
	var json_request = '{ "UserPhone":"' + userphone + '" , "UserPassword":"' + userpwd + '" , "UserName":"' + username + '" , "UserGender":"' + usergender + '" , "UserEmail":"' + useremail + '" }';
	xmlhttp.open("POST", "user.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send("Register=" + json_request);
}

function register() {
	var userphone = document.getElementsByName('UserPhoneRegister')[0].value;
	var userpwd = document.getElementsByName('UserPasswordRegister')[0].value;
	var repwd = document.getElementsByName('RePassword')[0].value;
	var username = document.getElementsByName('UserName')[0].value;
	var usergender = document.getElementsByName('UserGender')[0].value;
	var useremail = document.getElementsByName('UserEmailRegister')[0].value;
	if(userphone != '' && useremail == '' && username != '') {
		if(checkPhone(userphone)) {
			if(checkPassword(userpwd)) {
				if(userpwd == repwd) {
					ajaxRegister(userphone, userpwd, username, usergender, null);
				} else {
					alert("重复密码不一致");
				}
			} else {
				alert("请输入规范的密码");
			}
		} else {
			alert("请输入规范的手机号");
		}
	} else if(userphone != '' && useremail != '' && username != '') {
		if(checkPhone(userphone)) {
			if(checkEmail(useremail)) {
				if(checkPassword(userpwd)) {
					if(userpwd == repwd) {
						ajaxRegister(userphone, userpwd, username, usergender, useremail);
					} else {
						alert("重复密码不一致");
					}
				} else {
					alert("请输入规范的密码");
				}
			} else {
				alert("请输入规范的邮箱");
			}
		} else {
			alert("请输入规范的手机号");
		}
	} else if(userphone == '') {
		alert("手机号不能为空");
	} else if(username == '') {
		alert("昵称不能为空");
	}
}

function logout() {
	if(confirm("确认退出？")) {
		var xmlhttp;
		if(window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		} else {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				//			alert(xmlhttp.responseText);
				location.reload();
			}
		}
		xmlhttp.open("POST", "user.php", true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send("Action=LogOut");
	}
};

function postclick(){
	var topic_class_id = document.getElementsByName('topic-class-id')[0].value;
	var topic_title = document.getElementsByName('topic-title')[0].value;
	var topic_content = document.getElementsByName('topic-content')[0].value;
	if(topic_title!=''&&topic_content!=''){
		ajaxPosting(topic_class_id,topic_title,topic_content);
	}
	else if(topic_title==''){
		alert("请输入标题！");
	}
	else{
		alert("请输入正文内容！");
	}
}

function replyclick(){
	var reply_content = document.getElementsByName('reply-content')[0].value;
	var topics_topic_id = document.getElementsByName('topics-topic-id')[0].value;
	if(reply_content!=''){
		ajaxReplying(reply_content,topics_topic_id);
	}
	else{
		alert("请输入回复内容！");
	}
}

function ajaxPosting(topic_class_id, topic_title,topic_content){
	var xmlhttp;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText == 106) {
//				window.open('index.php', '_self');
				alert("发表成功！自动刷新页面！");
				location.reload();
			} else {
				alert("发表失败！错误信息：\n"+xmlhttp.responseText);
			}
		}
	}
	var json_request = '{ "TopicTitle":"' + topic_title + '" , "TopicContent":"' + topic_content + '" , "TopicClassId":"' + topic_class_id + '" }';
	xmlhttp.open("POST", "posting.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send("Publish=" + json_request);
}

function ajaxReplying(reply_content,topics_topic_id){
	var xmlhttp;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText == 106) {
//				window.open('index.php', '_self');
				alert("发表成功！自动刷新页面！");
				location.reload();
			} else {
				alert("发表失败！错误信息：\n"+xmlhttp.responseText);
			}
		}
	}
	var json_request = '{ "ReplyContent":"' + reply_content +  '" , "TopicsTopicId":"' + topics_topic_id + '" }';
	xmlhttp.open("POST", "posting.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send("Reply=" + json_request);
}
